package com.guoranhao.tool.model;

import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.io.Serializable;
import java.util.Set;

/**
 * Description:
 * All Rights Reserved.
 *
 * @version 1.0  2018/2/3 15:57  by  zhoudalin（zhoudl@cloud-young.com）创建
 */
public class BaseModel implements Serializable {
    private static Validator validator = Validation
            .byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

    public ValidateResult isValidate() {
        ValidateResult validateResult = new ValidateResult();
        validateResult.setValid(true);
        Set<ConstraintViolation<BaseModel>> constraintViolations = validator.validate(this);
        // 抛出检验异常

        if (constraintViolations.size() > 0) {
            validateResult.setValid(false);
            validateResult.setValidateMsg(constraintViolations.iterator().next().getMessage());
        }
        return validateResult;
    }
}